package com.unionx.unionxuf.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unionx.unionxuf.common.BillPram;
import com.unionx.unionxuf.common.TransformerStr;
import com.unionx.unionxuf.common.Zhuangtai;
import com.unionx.unionxuf.entity.Code;
import com.unionx.unionxuf.entity.ComBill;
import com.unionx.unionxuf.entity.ComBill2;
import com.unionx.unionxuf.entity.Credence;
import com.unionx.unionxuf.entity.CropDTD;
import com.unionx.unionxuf.entity.Excel;
import com.unionx.unionxuf.entity.Record;
import com.unionx.unionxuf.service.BillMainService;
import com.unionx.unionxuf.service.BillSubService;
import com.unionx.unionxuf.service.CredenceService;
import com.unionx.unionxuf.service.CustomerService;
import com.unionx.unionxuf.service.PersonService;
import com.unionx.unionxuf.service.RecordService;
import com.unionx.unionxuf.service.SubjectBindService;

@Controller
@RequestMapping(value = "/main")
public class MainPageController {

	@Autowired(required = true)
	private SubjectBindService subjecService;
    
	@Autowired(required = true)
	private CustomerService customerService;
	
	@Autowired(required = true)
	private PersonService personService;
	@Autowired(required = true)
	private CredenceService credenceService;
	@Autowired
	private BillMainService billmainser;
	@Autowired
	private BillSubService bsservice;
	@Autowired
	private  RecordService recordService;
	/**
	 * 获取结算方式信息
	 * 
	 * @return cSSCode：结算方式编码 cSSName ：名称，bSSEnd 是否末级
	 */
	@RequestMapping(value = "/get/settle")
	@ResponseBody
	public Object getSettle(HttpServletRequest request,Integer pageSize,Integer pageNum) {
		return subjecService.getSettle(request,pageSize,pageNum);

	}
	
	/**
	 * 获取结算方式编码
	 * 
	 * @return cSSCode：结算方式编码 cSSName ：名称，bSSEnd 是否末级
	 */
	@RequestMapping(value = "/get/settleinfo")
	@ResponseBody
	public Object getSettleinfo(HttpServletRequest request) {
		return subjecService.getSettleinfo(request);

	}

	/**
	 * 获取结算方式
	 * 
	 * @return cSSCode：结算方式编码 cSSName ：名称，bSSEnd 是否末级
	 */
	@RequestMapping(value = "/get/settles")
	@ResponseBody
	public Object getSettles(HttpServletRequest request) {
		return subjecService.getSettles(request);

	}
	
	
	/**
	 * 获取结算方式
	 * 
	 * @return cSSCode：结算方式编码 cSSName ：名称，bSSEnd 是否末级
	 */
	@RequestMapping(value = "/get/localsettles")
	@ResponseBody
	public Object getlocalSettles(String cropid) {
		return subjecService.getlocalSettles(cropid);

	}
	
	
	
	/**
	 * 根据scode获取结算方式
	 * 
	 *
	 */
	@RequestMapping(value = "/get/settlename")
	@ResponseBody
	public Object settlename(HttpServletRequest request,String scode) {
		
		System.out.println(scode);
		return  subjecService.getSettlename(request,scode);
	}
	
	/**
	 * 获取科目信息
	 * 
	 * @return ccode 科目编码 ccode_name 科目名称 bend是否末级
	 */
	@RequestMapping(value = "/get/subject")
	@ResponseBody
	public Object getSubject(HttpServletRequest request,Integer pageSize,Integer pageNum) {
		Map<String,Object> map = new HashMap<String,Object>();

		List<Code> list = new ArrayList<Code>();
		Map<String,Object> maps = (Map<String, Object>) subjecService.getSubject(request);
		list = (List<Code>) maps.get("Clist");
		int count = list.size();
		int total =0;
		int  re =0;
		
		List<Code> clist = new ArrayList<Code>();
		
		for(int i = (pageNum-1)*pageSize;i<list.size();i++){
			if(re==pageSize){
				break;
			}else{
			Code c = list.get(i);
			clist.add(c);
			re++;
			}
		}
		if(count<pageSize){
			total=1;
		}
		if(count%pageSize==0){
			total = count/pageSize;
		}
		if(count%pageSize>0){
			total =count/pageSize+1;
		}
		map.put("data", clist);
		map.put("total", total);
		
		return map;
	}
    
	/**
	 * 获取顾客信息
	 * @return cCusCode  客户编码，cCusAbbName客户简称
	 */
	@RequestMapping(value = "/get/customer")
	@ResponseBody
	public Object getCustomer(HttpServletRequest request) {
		return customerService.getCustomer(request);
	}
	
	
	/**
	 * 获取职员信息
	 * @return cPersonCode职员编码，cPersonName职员姓名
	 */
	@RequestMapping(value = "/get/person")
	@ResponseBody
	public Object getPerson(HttpServletRequest request){
		return personService.getPerson(request);
	}
	
	
	/**
	 * 根据code获取科目信息
	 * 
	 * @return ccode 科目编码 ccode_name 科目名称 bend是否末级
	 */
	@RequestMapping(value = "/get/subjectInfo")
	@ResponseBody
	public Object getSubjectInfo(HttpServletRequest request,String code) {
		
		Map<String,Object> map = new HashMap<String,Object>();
		Code c=subjecService.getSubjectInfo(request,code);
		map.put("code", c);
		return  map;
	}
	
	
	/**
	 * 获取科目方式编码
	 * 
	 * @return cSSCode：结算方式编码 cSSName ：名称，bSSEnd 是否末级
	 */
	@RequestMapping(value = "/get/subinfo")
	@ResponseBody
	public Object subinfo(HttpServletRequest request) {
		return subjecService.getsubinfo(request);

	}

	
	/**
	 * 根据subcode获取科目名称
	 * 
	 *
	 */
	@RequestMapping(value = "/get/subname")
	@ResponseBody
	public Object subname(HttpServletRequest request,String subcode) {
		return subjecService.getsubname(request,subcode);

	}
	
	/**
	 * 获取职员信息
	 * @return cPersonCode职员编码，cPersonName职员姓名
	 */
	@RequestMapping(value = "/get/persons")
	@ResponseBody
	public Object getPersons(HttpServletRequest request){
		return personService.getPersons(request);
	}
	
	
	
	/**
	 * 获取消费大类信息
	 * 
	 */
	@RequestMapping(value = "/get/xiaofei")
	@ResponseBody
	public Object xiaofei(HttpServletRequest request){
		return customerService.getXiaofei(request);
	}
	
	
	/**
	 * 获取本地消费大类信息
	 * 
	 */
	@RequestMapping(value = "/get/localxiaofei")
	@ResponseBody
	public Object localxiaofei(String cropid){
		return customerService.localgetXiaofei(cropid);
	}
	
	
	/**
	 * 获取消费大类下的自信息
	 * 
	 */
	@RequestMapping(value = "/get/zixiaofei")
	@ResponseBody
	public Object zixiaofei(HttpServletRequest request,String iname){
		return customerService.zixiaofei(request,iname);
	}
	
	/**
	 * 获取本地消费大类下的自信息
	 * 
	 */
	@RequestMapping(value = "/get/localzixiaofei")
	@ResponseBody
	public Object localzixiaofei(String iname){
		return customerService.localzixiaofei(iname);
	}
	
	
	//生成凭证
		@RequestMapping(value = "/save/cre")
		@ResponseBody
		public Object saveCredence(Credence credence){
			return credenceService.saveCredence(credence);
			
		}
		
	
		@RequestMapping(value = "/get/pros")
		@ResponseBody
		public Object getPros(String rows,String page){
			
			int row = Integer.parseInt(rows);//每页显示多少条
			int pa = Integer.parseInt(page);//当前页
			Map<String,Object> map = (Map<String, Object>) billmainser.getPros(row, pa);
			return map;
			
		}	
		
		@RequestMapping(value = "/get/skxbss")
		@ResponseBody
		public Object getBsList(String cropid,String serialno){
			
			return billmainser.getBillsubList(cropid, serialno);
		}
	
		
		@RequestMapping(value = "/get/skxbs")
		@ResponseBody
		public Object getSks(String cropid,String serialno){
		
			return billmainser.getBillamin(cropid, serialno);
			
			
		}	
		
		
	@RequestMapping(value = "/get/skxbb")
		@ResponseBody
		public Object getSkList(String cropid,String serialno){
		
			return bsservice.getBillSub(cropid,serialno);
			
			
		}
		
		
		@RequestMapping(value = "get/stinfo")
		@ResponseBody
		public Object getStList(String cropid,String serialno){
		
			return billmainser.getStList(cropid, serialno);
			
			
		}
		
		
		@RequestMapping(value = "get/rminfo")
		@ResponseBody
		public Object getRmList(String cropid,String serialno){
		
			return billmainser.getRmList(cropid, serialno);
		}
		
		@RequestMapping(value = "/get/pros2")
		@ResponseBody
		public Object getP(String rows,String page,String cropid,String cretype){
	
			BillPram b  = new BillPram();
				b.setCropid(cropid);
				b.setPagenum(Integer.parseInt(page));
				b.setPagesize(Integer.parseInt(rows));
				b.setZhuangtai(cretype);
				Map<String,Object> map = (Map<String, Object>) billmainser.getP(b);
				return map;
		}
		
		
		@RequestMapping(value = "/get/pros3")
		@ResponseBody
		public Object getP3(String rows,String page,String cropid,String riqi){
	
			Zhuangtai z  = new Zhuangtai();
				z.setCropid(cropid);
				z.setPagenum(Integer.parseInt(page));
				z.setPagesize(Integer.parseInt(rows));
				z.setZhuangtai("0");
				z.setRiqi(riqi);
				Map<String,Object> map = (Map<String, Object>) billmainser.getP3(z);
				return map;
		}
		
		
		@RequestMapping(value = "/get/pzinfos")
		@ResponseBody
		public Object getProbyLsandCids(HttpServletRequest request,String cropid,String serialno,String zhuangtai){
		
			return billmainser.getProbyLsandCids(request,cropid, serialno,zhuangtai);
			
			
		}
		
		@RequestMapping(value = "/get/pzinfo")
		@ResponseBody
		public Object getProbyLsandCid(HttpServletRequest request,String cropid,String serialno,String zhuangtai){
		
			return billmainser.getProbyLsandCid(request,cropid, serialno,zhuangtai);
			
			
		}
		
		
		@RequestMapping(value = "/get/pros4")
		@ResponseBody
		public Object getP4(HttpServletRequest request,String rows,String page,String cropid){
	
			
				return billmainser.getP4(request,Integer.parseInt(rows),Integer.parseInt(page),cropid);
		}
		
		
		
		@RequestMapping(value = "/save/record")
		@ResponseBody
	    private Object saveRecord(HttpServletRequest request,Record r){
			
			
			return recordService.saveRecord(request,r);
			
		}	
		
		
		
		@RequestMapping(value = "/save/records")
		@ResponseBody
	    private Object saveRecords(HttpServletRequest request,String cropid,String serialno,String zhuangtai,String g1,String g2,String g3,String g4,String g5){
			
			
			return recordService.saveRecords(request,cropid,serialno,zhuangtai,g1,g2,g3,g4,g5);
			
		}	
	
	
		@RequestMapping(value = "/get/cb")
		@ResponseBody
	    private Object getCB(HttpServletRequest request,ComBill2 cb,Integer rows,Integer page){
				
				/*cb.setPagenum(page);
				cb.setPagesize(rows);*/
				return recordService.getCB(request,cb);
			
		}	
		
		
		@RequestMapping(value = "/create/excel")
		@ResponseBody
	    private Object creteExcel(HttpServletRequest request, Excel ex){
			
			
			return recordService.creteExcel(request, ex);
			
		}	

	
		@RequestMapping(value = "/sc")
		@ResponseBody
	    private Object deleteBill(HttpServletRequest request, CropDTD c){
			Map<String,Object> maps = (Map<String, Object>) billmainser.deleteBill(request, c);
			
			return maps;
			
		}	
		
		@RequestMapping(value="/sh")
		@ResponseBody
		private Object shenheBill(CropDTD c){
				c.setZhuangtai("1");
			return billmainser.updateCtype2(c);
			
		}
		
		

}
